package com.vortex.cloud.sample.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vortex.cloud.sample.domain.TaskUser;
import com.vortex.cloud.sample.domain.TaskUserInfo;
import com.vortex.cloud.sample.dto.ExcelMessageTaskUserInfoDTO;
import com.vortex.cloud.sample.dto.TaskUserDemoExcelDTO;
import com.vortex.cloud.sample.service.ExcelDemoService;
import com.vortex.cloud.sample.service.ExcelService;
import com.vortex.cloud.sample.service.TaskUserService;
import com.vortex.cloud.sample.vo.TaskUserVo;
import com.vortex.cloud.vfs.cmmon.web.util.RestResultUtil;
import com.vortex.cloud.vfs.data.dto.RestResultDto;
import com.vortex.cloud.vfs.lite.base.dto.RestResultDTO;
import com.vortex.cloud.vfs.lite.base.dto.excel.ExcelMessageDTO;
import com.vortex.cloud.vfs.lite.base.dto.excel.ExcelReadDTO;
import com.vortex.cloud.vfs.lite.base.support.Constants;
import com.vortex.cloud.vfs.lite.base.util.ExcelUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-10-15 周五
 * @time 13:56
 * @description
 */
@Tag(name = "POI导入导出", description = "excel文件导入导出")
@Validated
@CrossOrigin
@RestController
@RequestMapping("cloud/sample/api/v101/task/poi")
public class ExcelController {

    @Resource
    private ExcelService excelService;

    @Resource
    private ExcelDemoService excelDemoService;

    @Operation(summary = "导入excel表")
    @RequestMapping(value = "importExcel", method = {RequestMethod.POST}, consumes = "multipart/form-data")
    public RestResultDto<List<ExcelMessageDTO>> importExcel(
            @Parameter(description = "文件", required = true) @RequestPart(required = false) MultipartFile file,
            @Parameter(description = "开始读取数据的行索引") @RequestParam(required = false, defaultValue = "1") Integer startRowNum,
            @Parameter(description = "开始读取数据的列索引") @RequestParam(required = false, defaultValue = "1") Integer startCellNum) {
        List<ExcelMessageDTO> messages = excelService.importExcel(file, startRowNum, startCellNum);
        if (CollectionUtils.isEmpty(messages)) {
            return RestResultUtil.handleSuccess();
        } else {
            RestResultDto restResultDto = new RestResultDto();
            restResultDto.setResult(RestResultDto.RESULT_FAIL);
            restResultDto.setData(messages);
            restResultDto.setMsg("导入失败");
            return restResultDto;
        }

    }

    @Operation(summary = "导入excel表")
    @RequestMapping(value = "importExcel22", method = {RequestMethod.POST}, consumes = "multipart/form-data")
    public RestResultDto<List<ExcelMessageDTO>> importExcel22(
            @Parameter(description = "文件", required = true) @RequestPart(required = false) MultipartFile file,
            @Parameter(description = "开始读取数据的行索引") @RequestParam(required = false, defaultValue = "1") Integer startRowNum,
            @Parameter(description = "开始读取数据的列索引") @RequestParam(required = false, defaultValue = "1") Integer startCellNum) {
        List<ExcelMessageDTO> messages = excelDemoService.importExcel(file, startRowNum, startCellNum);
        if (CollectionUtils.isEmpty(messages)) {
            return RestResultUtil.handleSuccess();
        } else {
            RestResultDto restResultDto = new RestResultDto();
            restResultDto.setResult(RestResultDto.RESULT_FAIL);
            restResultDto.setData(messages);
            restResultDto.setMsg("导入失败");
            return restResultDto;
        }
    }

    @Operation(summary = "导出Excel")
    @RequestMapping(value = "exportExcel", method = {RequestMethod.POST})
    public RestResultDTO<String> exportExcel(
//            @ParameterObject @SortDefault(sort = "createTime", direction = Sort.Direction.DESC) Sort sort,
//            @ParameterObject @RequestParam ExcelMessageTaskUserInfoDTO queryDTO,
            @Parameter(description = "导出列JSON") @RequestParam(required = false) String columnJson,
            @Parameter(description = "文件扩展名") @RequestParam(required = false,
                    defaultValue = Constants.EXTENSION_XLS) String extension,
            HttpServletResponse response) {
//        LambdaQueryWrapper<TaskUserInfo> lqw = Wrappers.lambdaQuery();
//        lqw.eq(Objects.nonNull(queryDTO.getId()), TaskUserInfo::getName, queryDTO.getId())
//                .eq(StringUtils.isNotBlank(queryDTO.getUserId()), TaskUserInfo::getName, queryDTO.getUserId())
//                .like(StringUtils.isNotBlank(queryDTO.getName()), TaskUserInfo::getName, queryDTO.getName());
//                .eq(StringUtils.isNotBlank(queryDTO.getEmail()), TaskUserInfo::getEmail, queryDTO.getEmail())
//                .eq(StringUtils.isNotBlank(queryDTO.getGender()), TaskUserInfo::getGender, queryDTO.getGender())
//                .eq(Objects.nonNull(queryDTO.getScore()), TaskUserInfo::getScore, queryDTO.getScore())
//                .eq(StringUtils.isNotBlank(queryDTO.getIp()), TaskUserInfo::getIp, queryDTO.getIp())
//                .eq(Objects.nonNull(queryDTO.getCount()), TaskUserInfo::getCount, queryDTO.getCount())
//                .eq(StringUtils.isNotBlank(queryDTO.getDate()), TaskUserInfo::getDate, queryDTO.getDate());

        List<TaskUserInfo> results = excelService.list(null);
        List<ExcelMessageTaskUserInfoDTO> taskVOList = results.stream().map(entity -> {
            ExcelMessageTaskUserInfoDTO vo = new ExcelMessageTaskUserInfoDTO();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        }).collect(Collectors.toList());

//        List<ExcelMessageTaskUserInfoDTO> taskVOList = new ArrayList<>();
//        ExcelMessageTaskUserInfoDTO vo = new ExcelMessageTaskUserInfoDTO();
////        vo.setId(1);
//        vo.setUserId("10002");
//        vo.setName("王琳琳");
//        vo.setEmail("12455");
//        vo.setGender("男");
//        vo.setCount(20);
//        vo.setIp("129.168.1.02");
//        vo.setScore(56);
//        vo.setStartDate("2021-10-14 16:53:07");
//        taskVOList.add(vo);

        ExcelUtils.exportExcel("用户数据", extension, null, columnJson, taskVOList, response);
        return RestResultDTO.newSuccess("导出成功");

    }

    private QueryWrapper<TaskUserInfo> buildQuery(ExcelMessageTaskUserInfoDTO params) {
        QueryWrapper<TaskUserInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(TaskUserInfo::getName, getName);
        if (StringUtils.isNotEmpty(params.getName())) {
            queryWrapper.lambda().like(TaskUserInfo::getName, params.getName());
        }
        return queryWrapper;
    }


    @Operation(summary = "导出Excel")
    @RequestMapping(value = "exportExcel22", method = {RequestMethod.POST})
    public RestResultDTO<String> exportExcel22(
//            @ParameterObject @SortDefault(sort = "createTime", direction = Sort.Direction.DESC) Sort sort,
//            @ParameterObject @RequestParam ExcelMessageTaskUserInfoDTO queryDTO,
            @Parameter(description = "导出列JSON") @RequestParam(required = false) String columnJson,
            @Parameter(description = "文件扩展名") @RequestParam(required = false, defaultValue = Constants.EXTENSION_XLS) String extension,
            HttpServletResponse response) {
        List<TaskUser> results = excelDemoService.list(null);
        List<TaskUserDemoExcelDTO> taskVOList = results.stream().map(entity -> {
            TaskUserDemoExcelDTO vo = new TaskUserDemoExcelDTO();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        }).collect(Collectors.toList());

        ExcelUtils.exportExcel("Demo用户数据", extension, null, columnJson, taskVOList, response);
        return RestResultDTO.newSuccess("导出成功");
    }

}
